<?php

function getEntityParameter($entity, $attr){
	$vals = read("knowledgebase/".$entity.".xml");
			
	foreach($vals as $item){
		if($item['tag'] == 'ENTITY' && $item['type'] == 'open'){
			$param = $item['attributes'];
			return $param[strtoupper($attr)];
		}
	}
}

function isCustomType($type){
	if(substr($type, 0, 1) == '[' && substr($type, strlen($type) - 1, 1) == ']'){
		return true;
	}else{
		return false;
	}
}

function getCustomType($ctype){
	
	$ctype = substr($ctype, 1, strlen($ctype) - 2);
	$file = "knowledgebase/".$ctype.".xml";
	$vals = read($file);
	
	foreach($vals as $item){
		if($item['tag'] == 'CTYPE' && $item['type'] == 'open'){
			$param = $item['attributes'];
			$id = $param['ID'];
			$type = $param['TYPE'];
			$obj = new ctype($id, $type);
		}
		
		if($item['tag'] == 'OPTION' && $item['type'] == 'complete'){
			$param = $item['attributes'];
			$display = $param['DISPLAY'];
			$value = $item['value'];
			$obj->add_option($value, $display);
		}
		
	}
	
	return $obj;
}

function getEntity($file){
	$file = "knowledgebase/".$file.".xml";
	$vals = read($file);
	
	$paramList = array();
			
	foreach($vals as $item){
		if($item['tag'] == 'ENTITY' && $item['type'] == 'open'){
			$param = $item['attributes'];
			$id = $param['ID'];
			$display = $param['DISPLAY'];
			$table = $param['TABLE'];
			$entity = new entity($id, $display);
			$entity->set_table($table);
		}
		
		if($item['tag'] == 'PARAM' && ($item['type'] == 'open' || $item['type'] == 'complete')){
			$param = $item['attributes'];
			$eparam = new param($param['ID'], $param['DISPLAY'], $param['TYPE']);
				$eparam->set_length($param['LENGTH']);
				$eparam->set_listing($param['LISTING']);
				$eparam->set_field($param['FIELD']);
				$eparam->set_primary($param['PRIMARY']);
				if(isCustomType($param['TYPE'])){
					$ctype = getCustomType($param['TYPE']);
					$eparam->set_option($ctype);
				}
			array_push($paramList, $eparam);
		}
	}
	
	$entity->set_params($paramList);
	
	return $entity;
}
?>
